local zaihui = fk.CreateSkill {
  name = "inker_zaihui",
  max_turn_use_time = 3,
}

local U = require "packages.utility.utility"

local spec = { ---@type TrigSkelSpec<fun(self: TriggerSkill, event: DamageEvent, target: ServerPlayer, player: ServerPlayer, data: DamageData):any>
  audio_index = { 1, 2 },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zaihui.name) and not player:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local use = room:askToUseVirtualCard(player, {
      name = "ex_nihilo",
      card_filter = {
        n = {1, #player:getCardIds("he")},
      },
      skill_name = zaihui.name,
      prompt = "#inker_zaihui-invoke",
      skip = true,
    })
    if use then
      local card = use.card
      event:setCostData(self, { cards = Card:getIdList(card), tos = use.tos, use = use, anim_type = (event == fk.Damage and "offensive" or "masochism") })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    local use = dat.use
    use.extra_data = use.extra_data or {}
    use.extra_data.zaihui_converted_num = #dat.cards
    room:useCard(dat.use)
  end
}

zaihui:addEffect(fk.Damaged, spec, { check_skill_limit = true })
zaihui:addEffect(fk.Damage, spec, { check_skill_limit = true })
zaihui:addEffect(fk.CardUsing, {
  mute = true,
  can_refresh = function(self, event, target, player, data)
    return target == player and data.card and table.contains(data.card.skillNames, zaihui.name) and data.extra_data and data.extra_data.zaihui_converted_num
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@inker_zaihui", data.extra_data.zaihui_converted_num)
  end
})
zaihui:addEffect(fk.AfterCardsMove, {
  is_delay_effect = true,
  anim_type = "offensive",
  audio_index = { 3, 4 },
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(zaihui.name) then return end
    local mark = player:getMark("@inker_zaihui")
    if mark == 0 then return false end
    for _, move in ipairs(data) do
      if move.from == player or (move.to == player and move.toArea == Card.PlayerHand ) then
        for _, info in ipairs(move.moveInfo) do
          if move.to == player or info.fromArea == Card.PlayerHand then
            return player:getHandcardNum() == mark
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getMark("@inker_zaihui")
    room:setPlayerMark(player, "@inker_zaihui", 0)
    local targets = room:getAlivePlayers(false)
    local to = targets[1]
    if #targets > 1 then
      to = room:askToChoosePlayers(player, {
        targets = targets,
        min_num = 1,
        max_num = 1,
        skill_name = zaihui.name,
        prompt = "#inker_zaihui-damage:::" .. U.ConvertNumber(mark),
        cancelable = false
      })[1]
    end
    if to then
      event:setCostData(self, { tos = { to }, num = mark })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    local to, num = dat.tos[1], dat.num
    local choice1 = "#inker_zaihui_choice1"
    local choice2 = "#inker_zaihui_choice2:::" .. U.ConvertNumber(num)
    local choice3 = "#inker_zaihui_choice3:" .. player.id
    local all_choices = {choice1, choice2, choice3}
    local choices = table.simpleClone(all_choices)
    for _ = 1, 2, 1 do
      local choiceindex = table.indexOf(choices, choice2)
      if choiceindex ~= -1 and #to:getCardIds("h") < num then
        table.remove(choices, choiceindex)
      end
      local choice = room:askToChoice(to, {
        choices = choices,
        skill_name = zaihui.name,
        cancelable = false,
        all_choices = all_choices
      })
      if choice == choice1 then
        room:damage{
          to = to,
          damage = 1,
          skillName = zaihui.name,
        }
      elseif choice == choice2 then
        room:askToUseVirtualCard(to, {
          name = "ex_nihilo",
          card_filter = {
            n = num,
            pattern = ".|.|.|hand",
          },
          skill_name = zaihui.name,
          prompt = "#inker_zaihui-use:::" .. num,
          cancelable = false,
        })
      elseif choice == choice3 then
        player:clearSkillHistory(zaihui.name)
      end
      table.removeOne(choices, choice)
      if to.dead then return end
    end
  end
})

Fk:loadTranslationTable{
  ["inker_zaihui"] = "再晖",
  [":inker_zaihui"] = "每回合限三次，当你造成或受到伤害后，你可以将任意张牌当作【无中生有】使用并记录转化数为X。自使用此牌时开始，下次你手牌数变为X后，你令一名角色依次选择两项：①受到1点伤害；②将X张手牌当作【无中生有】使用；③此技能视为未发动过。",

  ["#inker_zaihui-invoke"] = "再晖：你可以将任意张牌当【无中生有】使用，下次你的手牌数变动为相同数字后，令一名角色执行各种效果",
  ["#inker_zaihui-damage"] = "再晖：你须令一名依次选择两项：<br>受伤/将%arg张手牌当【无中生有】使用/此技能视为未发动过",
  ["#inker_zaihui-use"] = "再晖：你须将 %arg 张牌当【无中生有】使用",

  ["#inker_zaihui_choice1"] = "受到1点伤害",
  ["#inker_zaihui_choice2"] = "将%arg张手牌当作【无中生有】使用",
  ["#inker_zaihui_choice3"] = "%src的〖再晖〗视为未发动过",

  ["@inker_zaihui"] = "再晖",

  ["$inker_zaihui1"] = "晋道克昌，皆君之功，艾何窃之？",
  ["$inker_zaihui2"] = "一山难容二虎，二士争桃，唯一人饱腹。",
  ["$inker_zaihui3"] = "维才疏智短，拼得一死也要保住先帝基业！",
  ["$inker_zaihui4"] = "愿陛下忍数日之辱，臣欲使社稷危而复安，日月幽而复明。",
}

return zaihui